﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace HTComics.Data
{
    public class Search
    {
        private static void LoadDataFromFile()
        {
            DataSet dsSource = new DataSet();
            DataTable dtComic, dtIssues, dtConditionTypes;

            dtConditionTypes = new DataTable();
            dtConditionTypes.TableName = "Table1";
            dtConditionTypes.ReadXmlSchema(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\CondTypesSchema.xml"));
            dtConditionTypes.ReadXml(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\CondTypes.xml"));
            dsSource.Tables.Add(dtConditionTypes);

            dtIssues = new DataTable();
            dtIssues.TableName = "Table1";
            dtIssues.ReadXmlSchema(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\IssuesSchema.xml"));
            dtIssues.ReadXml(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\Issues.xml"));
            dsSource.Tables.Add(dtIssues);

            dtComic = new DataTable();
            dtComic.TableName = "Table1";
            dtComic.ReadXmlSchema(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\ComicSchema.xml"));
            dtComic.ReadXml(System.Web.HttpContext.Current.Server.MapPath(@"~\TempData\Comic.xml"));
            dsSource.Tables.Add(dtComic);         
        }

        public static DataTable SearchComics(string search)
        {
            StringBuilder objSql = new StringBuilder();

            objSql.Append("SP_SEARCH_COMIC ");
            objSql.Append("@COMIC_TITLE='" + search + "'");

            return Common.GetDataTable(objSql.ToString());
        }

        public static DataTable SearchIssues(string comicId)
        {
            StringBuilder objSql = new StringBuilder();

            objSql.Append("SP_SEARCH_ISSUES ");
            objSql.Append("@COMIC_ID=" + comicId);

            return Common.GetDataTable(objSql.ToString());
        }

        public static DataSet GetIssueDetails(string comicId, string issueNumber, string conditionId)
        {
            StringBuilder objSql = new StringBuilder();

            objSql.Append("SP_GET_ISSUE_DETAILS ");
            objSql.Append("@COMIC_ID=" + comicId + ",");
            objSql.Append("@ISSUE_NUMBER='" + issueNumber + "'");

            if (!string.IsNullOrEmpty(conditionId))
                objSql.Append(",@CONDITION_ID=" + conditionId);

            return Common.GetDataSet(objSql.ToString());
        }
    }
}
